﻿--#include "data\functions\Systems\pkSystem\PkZhanHun.lua" once
--#include "data\functions\Systems\pkSystem\PkExp.lua" once
--#include "data\config\misc\ReliveConfig.lua" once
--#include "RedActorRelive.lua" once
--#include "data\config\vip\VipPrivilegesCfg.lua" once

--检查是否不用显示杀人者名字（用星号代替)
function IsNotShowName(sysarg)
	local nSceneId = Actor.getSceneId(sysarg)
	for k, sId in ipairs(ReliveConfig.NoShowNameScene) do
		if nSceneId == sId then
			return true
		end
	end
	return false
end

--打开复活窗口
--nLiveTime: 窗口剩余时间
function OpenReliveDialog(sysarg, nLiveTime)
	--print("OpenReliveDialog")
	local nDropZhanHun = 0
	local nDropExp = 0
	
	local svar = Actor.getStaticVar(sysarg)	
	local data = svar.killerData	--被击杀信息
	if not data then 
		svar.killerData = {}
		data = svar.killerData
		data[6] = 2
	end
	local campId = data[2]
	local killerName = data[3]
	local killerType = data[6]
	
	if not killerName then killerName = "" end
	if not killerType then killerType = 2 end
	if killerType == 0 then
		nDropZhanHun = CalcOnKillDropZhanHun(sysarg)
		nDropExp = CalcOnKillDropExp(sysarg)
		data[7] = nDropZhanHun
		data[8] = nDropExp
	end
	
	if IsNotShowName(sysarg) then
		killerName = "******"
	end
	
	SendToClientReliveMsg(sysarg, killerType, campId, killerName, nDropZhanHun, nDropExp,nLiveTime)
	
end

--打开复活窗口不计算数值
function OpenReliveDialogNotCalc(sysarg,nLiveTime)
	--print("OpenReliveDialogNotCalc")
	local svar = Actor.getStaticVar(sysarg)	
	local data = svar.killerData	--被击杀信息
	if not data then 
		svar.killerData = {}
		data = svar.killerData
		data[6] = 2
	end
	local campId = data[2]
	local killerName = data[3]
	local killerType = data[6]
	local nDropZhanHun = data[7]
	local nDropExp = data[8]
	if not killerName then killerName = "" end
	if not killerType then killerType = 2 end
	if not nDropZhanHun then nDropZhanHun = 0 end
	if not nDropExp then nDropExp = 0 end

	if IsNotShowName(sysarg) then
		killerName = "******"
	end
	
	SendToClientReliveMsg(sysarg, killerType, campId, killerName, nDropZhanHun, nDropExp, nLiveTime)
end

--发送数据客户端打开复活界面
function SendToClientReliveMsg(sysarg, killerType, campId, killerName, nDropZhanHun, nDropExp, nLiveTime)
	--print("SendToClientReliveMsg")
	local nDropZhanHun = tonumber(nDropZhanHun)
	local nDropExp = tonumber(nDropExp)
	local nLiveTime = tonumber(nLiveTime)
	
	local nFlag = 12
	if Actor.hasMapAreaAttri(sysarg, aaNotHereRelive) then		--要判断是否禁止原地复活
		nFlag = 4
	end
	-- print("nFlag:"..nFlag)
	local npack = DataPack.allocPacket(sysarg,LogicCmd.miscSys.sysId,LogicCmd.miscSys.sub.sOpenReliveDialog)
	if npack == nil then return end
	DataPack.writeByte(npack,killerType)		--击杀者类型  0 玩家 1怪物 2其他
	DataPack.writeString(npack,killerName)		--击杀者名字
	DataPack.writeInt(npack,nDropZhanHun) 		--掉落战魂
	DataPack.writeInt(npack,nDropExp)			--掉落经验
	DataPack.writeInt(npack,nLiveTime)			--窗口存活剩余时间
	DataPack.writeByte(npack,nFlag)				--复活按钮显示
	DataPack.flush(npack)
end

---------------------------------------------原地复活-----------------------------------------------

--[[点击确定  原地复活按钮 type 复活类型  1复活石复活 2元宝复活 3安全复活 4原地复活---------------------------
目前只会在原地复活用到
]]
function GoReliveFunc(sysarg, type)
	if Actor.hasMapAreaAttri(sysarg, aaNotHereRelive) then		--要判断是否禁止原地复活
		safeResurrection(sysarg)
	end

	--改为满血满蓝满内功
	local maxhp = Actor.getIntProperty(sysarg,PROP_CREATURE_MAXHP)
	Actor.changeHp(sysarg,maxhp)
	local maxmp = Actor.getIntProperty(sysarg,PROP_CREATURE_MAXMP)
	Actor.changeMp(sysarg,maxmp)
	local maxinner = Actor.getUIntProperty(sysarg,PROP_ACTOR_MAX_INNER)
	Actor.setUIntProperty(sysarg,PROP_ACTOR_INNER,maxinner)
	
	--[[
	local buff = ReliveConfig.protectBuff
	if buff.type > 0 then
		Actor.addBuff(sysarg, buff.type, 1,1,1,buff.time,Lang.ScriptTips.b00009)
	end
	]]
	if ReliveConfig.protectBuffId > 0 then 		--刚刚复活，给一个几秒的保护，以免刚复活就立即被击杀
		Actor.addBuffById( sysarg, ReliveConfig.protectBuffId )
	end

	Actor.clearReliveTimeOut(sysarg)
	--版号服 暂时屏蔽
	--if Actor.getIntProperty(sysarg,PROP_ACTOR_PK_VALUE) >= GlobalConfig.nEnterJailPkValue then
	--	TranRedActorToRedSceen(sysarg,true)
	--else
		Actor.onRelive(sysarg)
	--end
	--清除击杀者信息
	ClearKillerData(sysarg)
end

--检测复活消耗（不使用）
function CheckReliveFunc(sysarg)
	local nType = tonumber(nType)
	if Actor.getIntProperty(sysarg,PROP_CREATURE_HP) == 0 then 
		if Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL) > ReliveConfig.limitLevel then
			-- 复活丹
			if Actor.getItemCount(sysarg, ReliveConfig.item.id, -1, -1) < ReliveConfig.item.count	or Actor.removeItem(sysarg,ReliveConfig.item.id,ReliveConfig.item.count,-1,-1,-1,Lang.LogTips.l00020, 34) < ReliveConfig.item.count then
				Actor.sendItemLack( sysarg, 0, ReliveConfig.item.id, ReliveConfig.item.count )
				return
			end
		end
		GoReliveFunc(sysarg, 1)
	end
end

------------------------------原地复活--------------------------------------
function OnPlaceRelive(sysarg)
	if Actor.hasMapAreaAttri(sysarg, aaMapNoAbilityNoRank) then   --不能原地复活 魔尊战场
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.mt00026, ttFlyTip)
		return
	end

	--print("OnPlaceRelive...")
	--要判断是否禁止原地复活
	if Actor.hasMapAreaAttri(sysarg, aaNotHereRelive) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.mt00026, ttTipmsgWindow)
		--local nLiveTime = Actor.getReliveTimeOut(sysarg)
		--OpenReliveDialogNotCalc(sysarg, nLiveTime)			--打开复活窗口不计算数值
		return
	end
	
	if not Actor.checkActorLevel(sysarg, ReliveConfig.limitLevel, 0) then 
		GoReliveFunc(sysarg, 4)
		return
	end

	local dVar = Actor.getDyanmicVar(sysarg)
	if not dVar.placeReliveTime then
		dVar.placeReliveTime = 0
	end

	--检查原地复活的CD，以免狂点浪费免费的次数
	local now = System.getCurrMiniTime()
	if now - dVar.placeReliveTime < 3 then			--复活CD为10秒，不能高速点击
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0057,ttFlyTip)
		return
	end
	dVar.placeReliveTime = now

	--检查免费复活次数,注意，免费次数可以通过多种方式增加，如VIP、节日活动等
	--[[
	if CheckActorDailyDoneTimes( sysarg, enDailyOpTimes_FreeLocalRelive, 0 ) then
		AddActorDailyDoneTimes( sysarg, enDailyOpTimes_FreeLocalRelive, 0, 1 ) 				--用掉1费复活次数
		GoReliveFunc(sysarg, 4)		--原地复活
	else
	--]]
		--[[如果没有复活丹和元宝
		if Actor.getItemCount(sysarg, ReliveConfig.item.id, -1, -1) < ReliveConfig.item.count then   --itemid为复活石的ID
			local itemName = Item.getItemName(ReliveConfig.item.id)
			--Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.f00003, itemName), ttFlyTip)
			Actor.sendItemLack( sysarg, 0, ReliveConfig.item.id, ReliveConfig.item.count )
			cancelFunc(sysarg)
			return 
		else
			if Actor.getItemCount(sysarg, ReliveConfig.item.id, -1, -1) >= ReliveConfig.item.count then
				CheckReliveFunc(sysarg)
			end
		end
		]]

		if Actor.checkActorLevel(sysarg, ReliveConfig.limitLevel, 0) then    --大于一定等级，原地复活需要扣除道具或者元宝
			--[[
			if not CheckConsumeOneOfTwo( sysarg, ReliveConfig.needitem, ReliveConfig.needYB ) then		--先扣除物品1，不足再扣除物品2
				return
			end

			if not DoConsumeOneOfTwo( sysarg, ReliveConfig.needitem, ReliveConfig.needYB, 34, Lang.LogTips.l00020 ) then
				return
			end
			--]]

			if not CheckConsumeCond( sysarg, {ReliveConfig.needYB}, 1) then
				return
			end

			if not DoConsumeCond( sysarg, {ReliveConfig.needYB}, 1, 34, Lang.LogTips.l00020  ) then
				return
			end
		end
		GoReliveFunc( sysarg, 1 )			--处理复活
	--end 

	SceneReliveDispatcher.dispatch( sysarg, 1 )			--处理额外的元宝、原地复活流程


	Hero.CallupBattleHero(sysarg)

	local actorName = Actor.getName( sysarg )
	local actorId 	= Actor.getActorId( sysarg )
	System.sendCommonLog( GameLog.clRelive, actorId, actorName, 1, 0, 0,'','', Lang.ScriptTips.Log001 )		--记录原地复活日志
	--print("OnPlaceRelive, actorName="..actorName)
end
--------------------------------------------------------------------------------------------------

------------------------------------安全复活-----------------------------------------------------

--回城复活（好像已经不使用了）
function backResurrection(sysarg)
	Actor.returnCity(sysarg)
	if Actor.getIntProperty(sysarg,PROP_CREATURE_HP) == 0 then 
		------------------------------------------------------------
		--掉落操作
		PkDropFunc(sysarg)
		
		--清除击杀者信息
		ClearKillerData(sysarg)
		----------------------------------------------------------
		
		--[[
		local maxhp = Actor.getIntProperty(sysarg,PROP_CREATURE_MAXHP)
		Actor.changeHp(sysarg,maxhp*ReliveConfig.safeRate)
		--local actorMp = Actor.getIntProperty(sysarg,PROP_CREATURE_MP)
		local maxmp = Actor.getIntProperty(sysarg,PROP_CREATURE_MAXMP)
		Actor.changeMp(sysarg,maxmp*ReliveConfig.safeRate)
		-- Actor.changeHp(sysarg,1)
		-- local actorMp = Actor.getIntProperty(sysarg,PROP_CREATURE_MP)
		-- Actor.changeMp(sysarg,-400)
		--]]
		--改为满血满蓝满内功
		local maxhp = Actor.getIntProperty(sysarg,PROP_CREATURE_MAXHP)
		Actor.changeHp(sysarg,maxhp)
		local maxmp = Actor.getIntProperty(sysarg,PROP_CREATURE_MAXMP)
		Actor.changeMp(sysarg,maxmp)
		local maxinner = Actor.getUIntProperty(sysarg,PROP_ACTOR_MAX_INNER)
		Actor.setUIntProperty(sysarg,PROP_ACTOR_INNER,maxinner)
	end
end

--安全复活-----------------------  
function safeResurrection(sysarg)
	--print("safeResurrection...")
	if Actor.getIntProperty(sysarg,PROP_CREATURE_HP) == 0 then
		------------------------------------------------------------
		--掉落操作
		PkDropFunc(sysarg)
		
		--改为满血满蓝满内功的30%恢复
		local maxhp = Actor.getIntProperty(sysarg,PROP_CREATURE_MAXHP)
		Actor.changeHp(sysarg, math.floor(maxhp*0.31))
		local maxmp = Actor.getIntProperty(sysarg,PROP_CREATURE_MAXMP)
		Actor.changeMp(sysarg, math.floor(maxmp*0.31))
		local maxinner = Actor.getUIntProperty(sysarg,PROP_ACTOR_MAX_INNER)
		Actor.setUIntProperty(sysarg,PROP_ACTOR_INNER,maxinner*0.31)
		
		--复活
		local boNotCheckRedName = false   --默认处理红名复活
		--[[把逻辑从 if not SendToFirstScene(sysarg) 中移处理，因为 SendToFirstScene()永远是false
		if not SendToFirstScene(sysarg) then
			
		end
		]]

		Hero.CallupBattleHero(sysarg)

		if SceneReliveDispatcher.dispatch( sysarg, 2 ) then 			--处理专门的安全复活流程流程
			return
		end

		--print("safeResurrection, after SceneReliveDispatcher.dispatch")

		if boNotCheckRedName == false then
			if GlobalConfig.nEnterJailPkValue >= 0 and Actor.getIntProperty(sysarg,PROP_ACTOR_PK_VALUE) >= GlobalConfig.nEnterJailPkValue then
				local RedNameC = {id = 1, x = 50, y = 50}	--土城红名村坐标
				System.telportScene(sysarg, RedNameC.id, RedNameC.x, RedNameC.y)
			else  
				Actor.relive(sysarg)
			end
		else
			Actor.onRelive(sysarg)
			--[[	一个游戏的阵营战只能有一个
			local zyId = Actor.getIntProperty( sysarg, PROP_ACTOR_ZY )
			local x,y
			if zyId == 1 then
				x = math.random(BLZConfig.ACampPos[1],BLZConfig.ACampPos[2])
				y = math.random(BLZConfig.ACampPos[3],BLZConfig.ACampPos[4])
			elseif zyId == 2 then
				x = math.random(BLZConfig.BCampPos[1],BLZConfig.BCampPos[2])
				y = math.random(BLZConfig.BCampPos[3],BLZConfig.BCampPos[4])
			end
			System.telportSceneByName(sysarg, BLZConfig.SceneName, x, y)
			]]
		end

		--清除复活倒计时
		Actor.clearReliveTimeOut(sysarg)
		
		------------------------------------------------------
		--阵营运镖 被劫
		--LootCampDart(sysarg)

		--帮派运镖 被劫
		--LootGuildDart(sysarg)

		--清除击杀者信息
		ClearKillerData(sysarg)

		--如果在行会副本处理
		--OnGuildFubenOp(sysarg)

		local actorName = Actor.getName( sysarg )
		local actorId 	= Actor.getActorId( sysarg )
		System.sendCommonLog( GameLog.clRelive, actorId, actorName, 2, 0, 0,'','', Lang.ScriptTips.Log002 )		--记录安全复活日志
		--print("safeResurrection, actorName="..actorName)
	end	
end

---------------------------------------------------------------------------

--购买复活石
function buyResurrecStone(sysarg)
	local nLiveTime = Actor.getReliveTimeOut(sysarg)
	OpenReliveDialogNotCalc(sysarg, nLiveTime)
	Actor.openDialogs(sysarg,diBuyAnItem,tostring(ReliveConfig.item.id))
end

--取消购买复活石 按钮的函数
function cancelFunc(sysarg)
	local nLiveTime = Actor.getReliveTimeOut(sysarg)
	OpenReliveDialogNotCalc(sysarg, nLiveTime)
end


--清除击杀者信息
function ClearKillerData(sysarg)
	--print("ClearKillerData")
	local svar = Actor.getStaticVar(sysarg)
	local data = svar.killerData	--被击杀者记录被杀信息
	if not data then 
		svar.killerData = {}
		data = svar.killerData
	end
	data[1] = nil	--击杀者等级
	data[2] = nil	--击杀者阵营ID
	data[3] = nil --击杀者名字
	data[6] = nil	--击杀者类型
	data[7] = nil
	data[8] = nil
end

--掉落操作
function PkDropFunc(sysarg)
	local svar = Actor.getStaticVar(sysarg)	
	local data = svar.killerData	--被击杀信息
	if(data == nil) then return end
	local killerName	 = data[3]	--击杀者名字
	local dropExpCount = data[4]	--每日PK经验掉落次数
	local killerType = data[6]
	local nDropZhanHun = data[7]
	local nDropExp = data[8]
	--print(nDropZhanHun)
	if killerType == 0 then
		if nDropZhanHun and nDropZhanHun > 0 then
			Actor.giveAward(sysarg, 10,0, -nDropZhanHun, 0, 0, 1, 0, 205, "OnKillDropZhanHun")
		end
		--[[	--屏蔽PK经验掉落、获得功能
		if nDropExp and nDropExp > 0 then
			local pkExpConfig = PkConfig.pkExpParam
			local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
			--掉落概率
			local percent = pkExpConfig.dropPercent[level - pkExpConfig.levelStart + 1]
			--print("percent:"..percent)
			if percent and percent > 0 then 
				--判断概率
				local rand = math.random(1000)
				--print("rand:"..rand)
				if rand <= percent then 
					--Actor.giveAward(sysarg, 10, 0,-nDropExp, 0, 0, 1, 0, 205, "OnKillDropZhanHun")
					if Actor.giveAward(sysarg, 2, 0,-nDropExp, 0, 0, 1, 0, 205, "OnKillDropExp") == true then
						if not dropExpCount then dropExpCount = 0 end
						data[4] = dropExpCount + 1
						Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.pk00001, killerName, nDropExp), ttTipmsgWindow)
						
						--如果被杀者有掉落经验 则击杀者获得经验
						if nDropExp > 0 then
							OnKillGetExp(sysarg, nDropExp)
						end
						
					end
				end
			end
		end
		--]]
	end
end
